/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.drypisy;

import java.awt.GridLayout;
import java.awt.image.BufferedImage;

/**
 *
 * @author Marcin
 */
public class SkewDialog extends javax.swing.JDialog {

    private SkewProcess proc = new SkewProcess();
    private BufferedImage image;
    private ImagePanel imgPanel;
    private AppControler ctrl;
    
    public SkewDialog(java.awt.Frame parent, boolean modal, BufferedImage img, AppControler c) {
        super(parent, modal);
        initComponents();
        ctrl = c;
        panel.setLayout(new GridLayout(1,1));
        imgPanel = ImagePanel.getSmallImagePanel(img);
        image = imgPanel.getImage();
        panel.add(imgPanel);
        setSize(image.getWidth()+ 30, image.getHeight() + 110);
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        sliderXAngle = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();
        sliderYAngle = new javax.swing.JSlider();
        panel = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        labelXangle = new javax.swing.JLabel();
        labelYangle = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(368, 337));

        jLabel1.setText("Kąt x");

        sliderXAngle.setMaximum(45);
        sliderXAngle.setMinimum(-45);
        sliderXAngle.setValue(0);
        sliderXAngle.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderXAngleStateChanged(evt);
            }
        });

        jLabel2.setText("Kąt y");

        sliderYAngle.setMaximum(45);
        sliderYAngle.setMinimum(-45);
        sliderYAngle.setValue(0);
        sliderYAngle.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderYAngleStateChanged(evt);
            }
        });

        panel.setMaximumSize(new java.awt.Dimension(2343424, 2343424));
        panel.setMinimumSize(new java.awt.Dimension(200, 200));
        panel.setPreferredSize(new java.awt.Dimension(450, 450));

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 348, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 351, Short.MAX_VALUE)
        );

        jButton1.setText("Pochyl");
        jButton1.setToolTipText("");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        labelXangle.setText("0");

        labelYangle.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(sliderXAngle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(sliderYAngle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(labelYangle, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                            .addComponent(labelXangle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(labelXangle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(sliderXAngle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(sliderYAngle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelYangle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void setWindowSize(int w, int h){
        int ow = getWidth();
        int oh = getHeight();
        int nw, nh;
        if(ow < w ){
            nw = w;
        }else{
            nw = ow;
        }
        if(oh < h ){
            nh = h;
        }else{
            nh = oh;
        }
        setSize(nw, nh);
    }
    
    private void sliderXAngleStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderXAngleStateChanged
        makeLookahead();
    }//GEN-LAST:event_sliderXAngleStateChanged

    private void sliderYAngleStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderYAngleStateChanged
        makeLookahead();
    }//GEN-LAST:event_sliderYAngleStateChanged

    private void makeLookahead(){
        double yAngle = (double)sliderYAngle.getValue();
        proc.setYAngle(yAngle);
        double xAngle = (double)sliderXAngle.getValue();
        proc.setXAngle(xAngle);
        labelXangle.setText(""+xAngle);
        labelYangle.setText(""+yAngle);
        BufferedImage tmpImage = proc.process(image);
        imgPanel.setImage(tmpImage);
        setWindowSize(tmpImage.getWidth()+ 30, tmpImage.getHeight() + 110);
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ctrl.setProcess(proc);
        ctrl.process();
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel labelXangle;
    private javax.swing.JLabel labelYangle;
    private javax.swing.JPanel panel;
    private javax.swing.JSlider sliderXAngle;
    private javax.swing.JSlider sliderYAngle;
    // End of variables declaration//GEN-END:variables
}
